import { createRouter, createWebHistory } from 'vue-router'
import layout from '@/layout/layout.vue';
import { ElMessage,ElNotification } from 'element-plus';
import {useStaffStaffStore} from "@/stores/staff.js";
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      {
          path: '/',
          component: layout,
          redirect:"index",
          children: [
              {
                  path: '/index',
                  name:"index",
                  component: () => import('../views/index.vue')
              },
              {
                  path: '/staff',
                  name:"staff",
                  component: () => import('../views/staff/staff.vue')
              },
              {
                  path: '/role',
                  name:"role",
                  component: () => import('../views/role/role.vue')
              },
              {
                  path: '/menu',
                  name:"menu",
                  component: () => import('../views/menu/menu.vue')
              },
              {
                path: '/organization',
                name:"organization",
                component: () => import('../views/organization/organization.vue')
            },
              {
                  path: '/staffInfo',
                  name:"staffInfo",
                  component: () => import('../views/staff/staffInfo.vue')
              },
          ]
      },
      {
          path:'/login',name:"login",component:()=>import("@/views/LoginView.vue")
      },
      // 404
      {
        path: "/found404",
        name: "found404",
        component: () => import("@/views/notFound.vue"),
      },
      {
        path: '/:pathMatch(.*)*',
        redirect: "/found404",
        hidden: true,
      },
  ]
})
router.beforeEach((to, from, next) => {
    if (localStorage.getItem("token") == null && to.path != '/login') {
        ElMessage.error('请先登录');
        router.push("/login")
    }else {
        if (to.path !== '/staffInfo'&&to.path !== '/login'&&useStaffStaffStore().staffInfo.staff.isFirstLogin==1) {
            ElNotification({
                title: '提示',
                message: '首次登陆，请修改密码',
                type: 'warning',
            });
            next('/staffInfo'); // 重定向到 /staffInfo
        } else{
            next()
        }

    }
});
export default router
